package springboot.springMVC;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springboot.springMVC.model.Person;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
//@RequestMapping("/test")
public class FirstPractice {
    @RequestMapping(value = "/m1",method = RequestMethod.POST)
    public Object getPerson(Person p){
        return p.toString();
    }


    /**
     * 为什么不能直接在PERSON使用@Data,会接收不到参数
     */

//    Spring官方升级导致的lombok失效，只要在pom文件注释掉一个插件，然后点击bundled maven3刷新maven idea,点击clean再重启项目
    @RequestMapping(value = "/m111",method = RequestMethod.POST)
    public Object setPerson(@RequestBody Person p){
        p.setName("zhang");
        p.setAge(12);
        return true;
    }

    @RequestMapping("/m2")
    public String sayHelo(){
        return "hello";
    }

    @RequestMapping("/m3")
    public String receive(@RequestParam("name1") String name) {//参数重命名
        return "接收到"+name;
    }
    @RequestMapping("/m4")
    public String receive2(@RequestParam(value = "name1",required = false) String name,int age) {//参数重命名
        return "接收到姓名："+name+" 年龄："+age;
    }
    @RequestMapping("/m5")
    public String array(String[] a){
        return "接收到数组"+ Arrays.toString(a);
    }
    @RequestMapping("/m6")
    public Object list1(@RequestParam List<String> list){//封装到集合，需要使用@requestparam
       return list.size();
    }
@RequestMapping("/m7")
    public Object json(@RequestBody Person p){
        return p.toString();
    }


@RequestMapping("/m8/{kg}/{m}")
    public String get(@PathVariable("kg") int a,@PathVariable int m){
        return "得到"+a+" "+m;
    }

    @RequestMapping("/m9")
    public String getFileName(@RequestPart("file") MultipartFile file) throws IOException {
    file.transferTo(new File("D:/temp/"+file.getOriginalFilename()));
    return file.getOriginalFilename();
    }

    @RequestMapping("/m10")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        StringBuilder a = new StringBuilder();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                a.append(cookie.getName() + ":" + cookie.getValue());
            }

        }
        return "获取到cookie"+a.toString();
    }


@RequestMapping("/m11")//使用开发者工具先设置cookie
    public String getCookie2(@CookieValue(value = "bite",defaultValue = "blank") String bite) {
          return "bite:"+bite;
    }



    @RequestMapping("/m12")
    public String setSession(HttpServletRequest a){
      // a.setAttribute("name","zhangsan");这个不是session属性
       HttpSession session = a.getSession();
       if(session != null){
           session.setAttribute("name","zhangsan");
           session.setAttribute("age",18);
           return "设置好session";
       }
       return "设置session失败";
    }

    /**
     * HttpServletResponse 主要用于处理响应，不涉及获取客户端的请求信息或管理会话。
     * 它的职责是将服务器处理的结果返回给客户端，而不是管理客户端和服务器之间的会话。
     * 服务器会在处理请求时根据请求对象 HttpServletRequest 来创建或获取会话，
     * 并可能在响应中通过 HttpServletResponse 将会话 ID 发送给客户端（通常通过 Cookie 或 URL 重写的方式）。
     * @param a
     * @return
     */


    @RequestMapping("/m13")//一定要先设置，再获取
    public String getSession(HttpServletRequest a){
        HttpSession request=a.getSession();
        if(request == null){
            return "获取session失败";
        }
        String response=request.getAttribute("name").toString()+request.getAttribute("age").toString();
        return response;
    }

    @RequestMapping("/m14")//如果session的name没赋值i，返回null
    public String getSession2(@SessionAttribute(value = "name",required = false)String name){
       return "name"+name;
    }

    @RequestMapping("/m15")
    public String getSession3(HttpSession a){
        return a.getAttribute("name").toString();
    }

    @RequestMapping("/m16")
    public String getHeader(HttpServletRequest a){
      // String userAgent= a.getHeader("hahahha");
        String userAgent= a.getHeader("User-Agent");//"User-Agent" 是几乎所有浏览器和客户端都会发送的请求头。
        return "name"+userAgent;
    }

    @RequestMapping("/m17")
    public String getHeader2(@RequestHeader("User-Agent")String userAgent){
        return "userAgent"+userAgent;
    }


}
